# -*- coding:utf-8 -*-

from mitmproxy import ctx
import json
from pymongo import MongoClient
from datetime import datetime



class DB(object):
    def __init__(self):
        self.client = MongoClient('127.0.0.1', 27017)
        self.db = self.client.DouYin
        self.collection = self.db.pinglun


    def insertItem(self, data):
        self.collection.insert_one(data)


db = DB()

# mitmdump.exe -p 8888 -s E:\Workspaces\Python\Spider\douyin\douyin_pinglun_script.py
# 所有发出的请求数据包都会被这个方法所处理
# 所谓的处理，我们这里只是打印一下一些项；当然可以修改这些项的值直接给这些项赋值即可
def request(flow):
    # # 获取请求对象
    # request = flow.request
    # # 实例化输出类
    # info = ctx.log.info
    # # 打印请求的url
    # info(request.url)
    # # 打印请求方法
    # info(request.method)
    # # 打印host头
    # info(request.host)
    # # 打印请求端口
    # info(str(request.port))
    # # 打印所有请求头部
    # info(str(request.headers))
    # # 打印cookie头
    # info(str(request.cookies))
    pass

# 所有服务器响应的数据包都会被这个方法处理
# 所谓的处理，我们这里只是打印一下一些项
def response(flow):
    # 获取响应对象
    response = flow.response
    # # 实例化输出类
    # info = ctx.log.info
    # # 打印响应码
    # info(str(response.status_code))
    # # 打印所有头部
    # info(str(response.headers))
    # # 打印cookie头部
    # info(str(response.cookies))
    # # 打印响应报文内容
    # info(str(response.text))

    # ctx.log.info(response.text)
    try:
        # 评论:
        if 'aweme/v2/comment/list' in flow.request.url:
            ctx.log.info("============获取数据=============")
            datajson = json.loads(response.text)
            for d in datajson["comments"]:
                data = dict()
                data['aweme_id'] = d['aweme_id']  # 视频ID
                data['cid'] = d['cid']  # 当前评论ID
                data["text"] = d['text']
                data['digg_count'] = d['digg_count']  # 点赞数量
                dateArray = datetime.fromtimestamp(int(d['create_time']))
                commentTime = dateArray.strftime("%Y-%m-%d %H:%M:%S")
                data['create_time'] = commentTime
                data['nickname'] = d['user']['nickname']
                data['uid'] = d['user']['uid']
                ctx.log.info(str(data))

                # db.insertItem(data)

                ctx.log.info("============Insert=============")

    except Exception as ex:
        ctx.log.warn("============错误=============")
        ctx.log.error(ex)





